% GAUTIER LE BIHAN  - 2020
% Replication files for �Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model� �Review of Economics and Statistics
% File: figure_hist_moments_appendix.m 
% Generates Figures A & B in Appendix

clear all
close all
clear matrix
clc


mat_actual_moments=xlsread('..\..\Moments\stat_94_2014_kurto.xlsx') ;
names_of_actual_moments={'p_c5','pond_c5','idposte1','m_kur','m_interq','m_q2','s_kur','s_interq','s_q2','fr_p','f_p','f','sfr_p','sf_p','sf','idposte_p','secteur'};

%choose moments to match with simulated moments%
my_list_actual={'secteur';'f';'fr_p';'m_q2';'m_interq';'m_kur';'sf';'sfr_p';'s_q2';'s_interq';'s_kur'};
n_actual_moments=size(my_list_actual,1);
%%Initialize vector of indices
idxa=zeros( size(my_list_actual,1),1) ;

%%Loop over selected moments
for ii=1:size(my_list_actual,1)
my_name=my_list_actual(ii);
%% This picks up the index corresponding to the selected moments (using string comparison function 'strcmp')
idxa(ii) = find(strcmp([names_of_actual_moments],my_name )) ;
end
       actual_moments=mat_actual_moments(:,idxa);
      % actual_moments=actual_moments(actual_moments(:,1)<0.5,:);
weight=mat_actual_moments(:,2);
sum(weight);
% for ii=1:size(my_list_actual,1)
% actual_moments_w(ii) = weightedMedian(actual_moments(:,ii),weight);
% end
 

test=[actual_moments weight];
sampl_sim=test(( test(:,1) ~= 3 & test(:,1) ~= 0),:);
%sampl_sim=test((test(:,6) == 5 ),:);
%test(:,6) ~= 5 &%

actual_moments_k=[sampl_sim];

%save actual_moments_en actual_moments_en

weight=sampl_sim(:,12);

actual_moments=actual_moments_k

%save actual_moments_k actual_moments_k

w=weight;



%freq
max=0.8;
min=0.0;
bins=80;
binValues_f=(min:(max-min)/bins:max);
[histw histv] = histwv(actual_moments((actual_moments(:,1)==1),2), w(actual_moments(:,1)==1,:), min, max, bins+1) 
counts1 = histw ;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==2),2), w(actual_moments(:,1)==2,:), min, max, bins+1) 
counts2 =  histw ;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==4),2), w(actual_moments(:,1)==4,:), min, max, bins+1) 
counts4 = histw;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==5),2), w(actual_moments(:,1)==5,:), min, max, bins+1) 
counts5 = histw;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==6),2), w(actual_moments(:,1)==6,:), min, max, bins+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_af=counts_a./sum;
counts_bf=counts_b./sum;
counts_cf=counts_c./sum;
countsf=counts_all./sum;

%share of increases
max_p=0.99;
min_p=0.1;
bins_p=60;
binValues_p=(min_p:(max_p-min_p)/bins_p:max_p);
[histw histv] = histwv(actual_moments((actual_moments(:,1)==1),3), w(actual_moments(:,1)==1,:), min_p, max_p, bins_p+1) 
counts1 = histw ;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==2),3), w(actual_moments(:,1)==2,:), min_p, max_p, bins_p+1) 
counts2 =  histw ;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==4),3), w(actual_moments(:,1)==4,:), min_p, max_p, bins_p+1) 
counts4 = histw;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==5),3), w(actual_moments(:,1)==5,:), min_p, max_p, bins_p+1) 
counts5 = histw;
[histw histv] = histwv(actual_moments((actual_moments(:,1)==6),3), w(actual_moments(:,1)==6,:), min_p, max_p, bins_p+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
% Histogram Sum �d1�+�d2�
counts_ap=counts_a./sum;
counts_bp=counts_b./sum;
counts_cp=counts_c./sum;
countsp=counts_all./sum;



%median
max_m=0.16;
min_m=0.0;
bins_m=50;
x=actual_moments((actual_moments(:,5)<max_m),5);
size(x)
wi=weight((actual_moments(:,5)<max_m),1);
size(wi)
s=actual_moments((actual_moments(:,5)<max_m),1);
size(s)
binValues_m=(min_m:(max_m-min_m)/bins_m:max_m);
[histw histv] = histwv(x((s==1)), wi(s==1,:), min_m, max_m, bins_m+1) 
counts1 = histw ;
[histw histv] = histwv(x((s(:,1)==2)), wi(s==2,:), min_m, max_m, bins_m+1) 
counts2 =  histw ;
[histw histv] = histwv(x((s(:,1)==4)), wi(s==4,:), min_m, max_m, bins_m+1) 
counts4 = histw;
[histw histv] = histwv(x((s(:,1)==5)), wi(s==5,:), min_m, max_m, bins_m+1) 
counts5 = histw;
[histw histv] = histwv(x((s(:,1)==6)), wi(s==6,:), min_m, max_m, bins_m+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_a_m=counts_a./sum;
counts_b_m=counts_b./sum;
counts_c_m=counts_c./sum;
counts_m=counts_all./sum;


%median
max_i=0.2;
min_i=0.0;
bins_i=50;
x=actual_moments((actual_moments(:,5)<max_i),5);
size(x)
wi=weight((actual_moments(:,5)<max_i),1);
size(wi)
s=actual_moments((actual_moments(:,5)<max_i),1);
size(s)
binValues_i=(min_i:(max_i-min_i)/bins_i:max_i);
[histw histv] = histwv(x((s==1)), wi(s==1,:), min_i, max_i, bins_i+1) 
counts1 = histw ;
[histw histv] = histwv(x((s(:,1)==2)), wi(s==2,:), min_i, max_i, bins_i+1) 
counts2 =  histw ;
[histw histv] = histwv(x((s(:,1)==4)), wi(s==4,:), min_i, max_i, bins_i+1) 
counts4 = histw;
[histw histv] = histwv(x((s(:,1)==5)), wi(s==5,:), min_i, max_i, bins_i+1) 
counts5 = histw;
[histw histv] = histwv(x((s(:,1)==6)), wi(s==6,:), min_i, max_i, bins_i+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_a_i=counts_a./sum;
counts_b_i=counts_b./sum;
counts_c_i=counts_c./sum;
counts_i=counts_all./sum;

%median
max_k=10;
min_k=2;
bins_k=50;
x=actual_moments((actual_moments(:,6)<max_k),6);
size(x)
wi=weight((actual_moments(:,6)<max_k),1);
size(wi)
s=actual_moments((actual_moments(:,6)<max_k),1);
size(s)
binValues_k=(min_k:(max_k-min_k)/bins_k:max_k);
[histw histv] = histwv(x((s==1)), wi(s==1,:), min_k, max_k, bins_k+1) 
counts1 = histw ;
[histw histv] = histwv(x((s(:,1)==2)), wi(s==2,:), min_k, max_k, bins_k+1) 
counts2 =  histw ;
[histw histv] = histwv(x((s(:,1)==4)), wi(s==4,:), min_k, max_k, bins_k+1) 
counts4 = histw;
[histw histv] = histwv(x((s(:,1)==5)), wi(s==5,:), min_k, max_k, bins_k+1) 
counts5 = histw;
[histw histv] = histwv(x((s(:,1)==6)), wi(s==6,:), min_k, max_k, bins_k+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_a_k=counts_a./sum;
counts_b_k=counts_b./sum;
counts_c_k=counts_c./sum;
counts_k=counts_all./sum;


figure;
subplot(1,2,1);
bar(binValues_f, countsf, 'r', 'barwidth', 1)
hold on
bar(binValues_f, counts_af, 'k', 'barwidth', 1)
hold on
bar(binValues_f, counts_bf, 'g', 'barwidth', 1)
hold on
bar(binValues_f, counts_cf, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
 legend('Location','best')
axis([min max 0 0.12])
  xlabel('Frequency of price changes');
 ylabel('[%]'); 

subplot(1,2,2);

 bar(binValues_p, countsp, 'r', 'barwidth', 1)
hold on
bar(binValues_p, counts_ap, 'k', 'barwidth', 1)
hold on
bar(binValues_p, counts_bp, 'g', 'barwidth', 1)
hold on
bar(binValues_p, counts_cp, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
axis([min_p max_p 0 0.12])
  xlabel('Share of price increases');
  legend('Location','northwest')
 ylabel('[%]'); 
 
print('..\..\..\figures\freq_stat_slide.pdf','-dpdf','-fillpage')
% %print('freq_stat','-depsc')
 
 %median
max_k=10;
min_k=2;
bins_k=50;
x=actual_moments((actual_moments(:,6)<max_k),6);
size(x)
wi=weight((actual_moments(:,6)<max_k),1);
size(wi)
s=actual_moments((actual_moments(:,6)<max_k),1);
size(s)
binValues_k=(min_k:(max_k-min_k)/bins_k:max_k);
[histw histv] = histwv(x((s==1)), wi(s==1,:), min_k, max_k, bins_k+1) 
counts1 = histw ;
[histw histv] = histwv(x((s(:,1)==2)), wi(s==2,:), min_k, max_k, bins_k+1) 
counts2 =  histw ;
[histw histv] = histwv(x((s(:,1)==4)), wi(s==4,:), min_k, max_k, bins_k+1) 
counts4 = histw;
[histw histv] = histwv(x((s(:,1)==5)), wi(s==5,:), min_k, max_k, bins_k+1) 
counts5 = histw;
[histw histv] = histwv(x((s(:,1)==6)), wi(s==6,:), min_k, max_k, bins_k+1) 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts5+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_a_k=counts_a./sum;
counts_b_k=counts_b./sum;
counts_c_k=counts_c./sum;
counts_k=counts_all./sum;


figure;
subplot(1,2,1);
bar(binValues_f, countsf, 'r', 'barwidth', 1)
hold on
bar(binValues_f, counts_af, 'k', 'barwidth', 1)
hold on
bar(binValues_f, counts_bf, 'g', 'barwidth', 1)
hold on
bar(binValues_f, counts_cf, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
 legend('Location','best')
axis([min max 0 0.12])
  xlabel('Frequency of price changes');
 ylabel('[%]'); 

subplot(1,2,2);

bar(binValues_m, counts_m, 'r', 'barwidth', 1)
hold on
bar(binValues_m, counts_a_m, 'k', 'barwidth', 1)
hold on
bar(binValues_m, counts_b_m, 'g', 'barwidth', 1)
hold on
bar(binValues_m, counts_c_m, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
axis([min_m max_m 0 0.12])
  xlabel('Median of price changes');
 ylabel('[%]'); 
print('..\..\..\figures\freq_size_slide.pdf','-dpdf','-fillpage')
% %print('freq_stat','-depsc')
 
 figure;
subplot(1,3,1);
bar(binValues_m, counts_m, 'r', 'barwidth', 1)
hold on
bar(binValues_m, counts_a_m, 'k', 'barwidth', 1)
hold on
bar(binValues_m, counts_b_m, 'g', 'barwidth', 1)
hold on
bar(binValues_m, counts_c_m, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
axis([min_m max_m 0 0.1])
  xlabel('Median of price changes');
 ylabel('[%]'); 
 subplot(1,3,2);
bar(binValues_i, counts_i, 'r', 'barwidth', 1)
hold on
bar(binValues_i, counts_a_i, 'k', 'barwidth', 1)
hold on
bar(binValues_i, counts_b_i, 'g', 'barwidth', 1)
hold on
bar(binValues_i, counts_c_i, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
axis([min_i max_i 0 0.1])
  xlabel('Interquartile');
 ylabel('[%]'); 
 
  subplot(1,3,3);
bar(binValues_k, counts_k, 'r', 'barwidth', 1)
hold on
bar(binValues_k, counts_a_k, 'k', 'barwidth', 1)
hold on
bar(binValues_k, counts_b_k, 'g', 'barwidth', 1)
hold on
bar(binValues_k, counts_c_k, 'b', 'barwidth', 1)
hold off
legend('Services', 'Energy', 'Manuf', 'Food')
axis([min_k max_k 0 0.1])
  xlabel('Kurtosis');
 ylabel('[%]'); 
 
print('..\..\..\figures\size_stat_slide.pdf','-dpdf','-fillpage')
 
